/* 
 * File:   main.c
 * Author: bjorn
 *
 * Created on September 2, 2007, 10:53 AM
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "screen.h"
#include "vector.h"
#include "maplayer.h"
#include "resource.h"
#include "rope.h"
#include "script.h"


int eruta_init() {
    int ok;
    ok = SDL_Init(SDL_INIT_EVERYTHING);
    if(!ok) return -1; 
    return 0;
}

int eruta_done() {
    SDL_Quit();
    return 0;
}

int test_screen() {
    Image tile1, tile2;
	int xindex, ind1, ind2, before, after;
	int yindex     ;
	Maplayer layer = maplayer_make(100,100);
    Screen screen  = screen_make(640, 480, 16, 1);
    if(!screen) return -1;
    tile1 = screen_make_image(screen, 32, 32);
    tile2 = screen_make_image(screen, 32, 32);
    image_fill_all(tile1, 0,     0, 255);
    image_fill_all(tile2, 0,   255, 0);
    ind1       = maplayer_add(layer, tile1);
    ind2       = maplayer_add(layer, tile2);
    
    for(xindex = 0; xindex < 100; xindex ++)    {
        for(yindex = 0; yindex < 100; yindex ++)    {
            maplayer_set(layer,xindex, yindex, ind1);
            if(xindex == yindex) { 
                maplayer_set(layer, xindex, yindex, ind2 );
            }
        }
    }
    before = SDL_GetTicks();
    for(xindex = 0; xindex < 1000; xindex ++) {
        maplayer_draw(layer, screen, xindex, xindex); 
        maplayer_draw(layer, screen, xindex, xindex); 
        maplayer_draw(layer, screen, xindex, xindex); 
        maplayer_draw(layer, screen, xindex, xindex); 
        screen_flip(screen);
    }
    after = SDL_GetTicks();
    printf("1000 renders in %d time: %f FPS", after - before, (1000000.0/(after-before)));
    return 0;
}

#ifdef _NO_WAY

struct Function_struct {
	struct Function_struct * (*call)(struct Function_struct *, void * argument)
	struct Function_struct * caller;
	eruta_pointer * scope;
	jmp_buf exception;
	double numparam;	
};

Function f_call_from(Function callern, Function callee, Function argument) {
	callee->caller = caller;
	return callee->call(argument);
}

#define F_OK 		1
#define F_EXCEPTION 0

int f_try(Function f) {
	res = setjmp(f->exception);
	if(!res) {
		return F_OK;
	} else {
		// Exception occurred
		return F_EXCEPTION;
	}
}

int f_throw(Function f, int value) {
	longjmp(f->exception, value);
}


typedef struct Function_struct * Function;
typedef struct Function (*Functionpointer)(Function f, void * argument);

Function f_wrap(Functionpointer call) {
	Function	 f 	=	 malloc(sizeof(Function_struct));
	f->call 		= call; 
}

Function f_identity(Function f) {
	return f_identity;
}

Function f_double_const(double d) {

		
}

/*
Function f_add(Function f) {
	((Functionpointer)f->call)();
}
*/
#endif

int test() {
    Rope getstr;
	Resource resource;
    Script script = script_make();
	Rope s1 = rope_make("Hello ");
    Rope s2 = rope_make("World!");
    Rope s3 = rope_make_join(s1, s2);
	Image angtil = image_load_c("share/image/angband_tiles_32x32.png");
	script_eval_c(script, "a={}");
	script_eval_c(script, "a.a=\"Lua Lua Lua!\"");
    script_eval_c(script, "print(a.a)");
    getstr = script_get_rope(script, "a.a", "Default Value");
    printf("Got string from LUA: %s\n", rope_c(getstr)); 
    /*
	* printf("Resource dir %s exists: %d\n", RESOURCE_DIRECTORY, file_exists(RESOURCE_DIRECTORY));
	*/
    printf("Rope %s %s %s\n", rope_c(s1) , rope_c(s2), rope_c(s3));
	rope_free(getstr);    
    rope_free(s3);    
    rope_free(s2);
    rope_free(s1); 
    
    if(!angtil) printf("Image not loaded!");
    else { printf("Image OK\n");  image_free(angtil);  }
	resource = resource_make();
	if(!resource) printf("Resource not loaded!");
	else {
		Image angtil2;	
		getstr = script_get_rope(resource->script, "ERUTA_TEST", "Default Value");
		printf("Got rope from LUA: %s\n", rope_c(getstr)); 
		rope_free(getstr); 
		angtil2 = resource_loadimage_c(resource, "angband_tiles_32x32.png");
		if(!angtil2) printf("Image 2 not loaded!");
    	else { printf("Image 2 OK\n");  image_free(angtil2);  }
		
	}
	script_free(script); 
	resource_free(resource);
    return 0;
}

/*
 * 
 */
int main(int argc, char* argv[]) {
    
    printf("Starting eruta... ");
    if(!eruta_init()) { printf("init FAILED!\n"); return -1; }
    printf("OK!\n");
    
    

    test();
    /*

    */
    /*
    Vector matr  = vector_make(sizeof(Vector), 100);
    size_t stop = vector_size(matr);
    for(size_t index = 0; index < stop; index ++ ) {
         vector_set(matr, index, vector_make(sizeof(int), 100));            
    }

    Vector vect  = vector_make(sizeof(char *), 100);
    vector_set(vect, 22, "Hello");
    vector_set(vect, 33, "World");
    
    Vector row = vector_get(matr, 10);
    vector_set(row, 10, 69);
    
    printf("%d %d; %d\n", screen_wide(screen), screen_high(screen), screen_tester_get(screen));
    printf("%s %s \n", vector_get(vect, 22), vector_get(vect, 33));
    
    vector_each(matr, (vector_each_callback) &vector_wipe);
    vector_free(matr);
    */

    
    eruta_done(); 
    
    return (EXIT_SUCCESS);
}

